<template>
  <div class="grade-management">
    <div class="page-header">
      <h2>成绩管理</h2>
      <div class="header-actions">
        <el-input
            v-model="searchKeyword"
            placeholder="请输入学生姓名、课程名称或学号"
            style="width: 300px; margin-right: 15px"
            clearable
            @keyup.enter="searchGrades"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary" @click="searchGrades">
          <el-icon><Search /></el-icon>
          搜索
        </el-button>
      </div>
    </div>

    <!-- 成绩列表 -->
    <el-table
        :data="gradeList"
        border
        stripe
        v-loading="loading"
        style="width: 100%"
    >
      <el-table-column prop="studentNo" label="学号" width="120" />
      <el-table-column prop="studentName" label="学生姓名" width="120" />
      <el-table-column prop="courseName" label="课程名称" width="150" />
      <el-table-column prop="score" label="成绩" width="80" />
      <el-table-column prop="createTime" label="录入时间" width="180">
        <template #default="scope">
          {{ formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getGradeList } from '../api/grade'

// 格式化日期
const formatDate = (dateTime) => {
  if (!dateTime) return ''
  const date = new Date(dateTime)
  return date.toLocaleString()
}

export default {
  name: 'GradeManagement',
  setup() {
    const loading = ref(false)
    const searchKeyword = ref('')
    const gradeList = ref([])
    const currentPage = ref(1)
    const pageSize = ref(10)
    const total = ref(0)

    // 获取成绩列表
    const fetchGradeList = async () => {
      loading.value = true
      try {
        const response = await getGradeList({
          page: currentPage.value,
          size: pageSize.value,
          keyword: searchKeyword.value
        })

        if (response.code === 200) {
          gradeList.value = response.data.records
          total.value = response.data.total
        }
      } catch (error) {
        console.error('获取成绩列表失败:', error)
        ElMessage.error('获取成绩列表失败')
      } finally {
        loading.value = false
      }
    }

    // 搜索成绩
    const searchGrades = () => {
      currentPage.value = 1
      fetchGradeList()
    }

    // 分页处理
    const handleSizeChange = (val) => {
      pageSize.value = val
      fetchGradeList()
    }

    const handleCurrentChange = (val) => {
      currentPage.value = val
      fetchGradeList()
    }

    onMounted(() => {
      fetchGradeList()
    })

    return {
      loading,
      searchKeyword,
      gradeList,
      currentPage,
      pageSize,
      total,
      searchGrades,
      handleSizeChange,
      handleCurrentChange,
      formatDate
    }
  }
}
</script>

<style scoped>
.grade-management {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-actions {
  display: flex;
  align-items: center;
}
</style>